package net.bingosoft.common.struts.tags;

import java.io.IOException;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.jsp.JspException;
import javax.servlet.jsp.JspWriter;
import javax.servlet.jsp.tagext.TagSupport;

import net.bingosoft.common.util.FileHelper;

/**
 * 2008-4-10 下午02:30:30
 * Author:yangdongyu(yangdy@bingosoft.net)
 * ReadMe:文件下载的标签
 * ChangeLog:
 */
public class FileDownloadTag extends TagSupport {

	private static final long serialVersionUID = 1L;
	private String fileName;
	private String filePath;
	private String imageURL;
	private Boolean isRelative = false;
	
	public String getFileName() {
		if(this.fileName == null && this.filePath!=null){
			this.fileName = FileHelper.getFileName(filePath);
		}
		return fileName;
	}
	public void setFileName(String fileName) {
		this.fileName = fileName;
	}
	public String getFilePath() {
		return filePath;
	}
	public void setFilePath(String filePath) {
		this.filePath = filePath;
	}
	public Boolean getIsRelative() {
		return isRelative;
	}
	public void setIsRelative(Boolean isRalative) {
		this.isRelative = isRalative;
	}

	@Override
	public int doStartTag() throws JspException {
		return super.doStartTag();
	}
	@Override
	public int doAfterBody() throws JspException {
		return super.doAfterBody();
	}
	
	public String getImageURL() {
		return imageURL;
	}
	public void setImageURL(String imageURL) {
		this.imageURL = imageURL;
	}
	@Override
	public int doEndTag() throws JspException {
		JspWriter writer = pageContext.getOut();
		try {
			StringBuilder sb = new StringBuilder();
			  sb.append("<nobr>&nbsp;&nbsp;&nbsp;")
				.append(" <a style='text-decoration: none' ")
				.append(" href=\"javascript:doDownloadFile(")
				.append("'").append(filePath).append("',")
				.append("'").append(((HttpServletRequest)pageContext.getRequest()).getContextPath()).append("',")
				.append(isRelative)
				.append(");\"> ")
				.append(getFileName());
				if(imageURL != null){
					sb.append(" <img alt='下载附件' src='")
					.append(imageURL)			
					.append("'>");
					
				}
				sb.append("</a></nobr> ");
			writer.print(sb);
		} catch (IOException e) {
			e.printStackTrace();
		}
        return super.doEndTag();
	}

}
